{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Arithmetic Binary Tree（算术二叉树）](https://mp.weixin.qq.com/s/eMx3BoJSuMsJb0m-Gv_L0w)\n",
    "\n",
    "You are given a binary tree representation of an arithmetic expression. In this tree, each leaf is an integer value, and a non-leaf node is one of the four operations: `+`, `-`, `*`, or `/`.\n",
    "\n",
    "给定一棵表达了数学运算表达式的二叉树，树中的每一片叶子都是一个整数，每一个非叶子节点都是一个四则运算符。\n",
    "\n",
    "Write a function that takes this tree and evaluates the expression.\n",
    "\n",
    "编写一个函数，将算术二叉树转换为数学表达式并求出运算结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example（示范样例）\n",
    "\n",
    "```text\n",
    "#      *\n",
    "#    /   \\\n",
    "#   +     +\n",
    "#  / \\   / \\\n",
    "# 3   2 4   5\n",
    "```\n",
    "\n",
    "This is a representation of the expression $(3 + 2) \\times (4 + 5)$, and should return $45$.\n",
    "\n",
    "这棵树描述了表达式$(3+2)\\times(4+5)$，运算结果应为$45$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Class Definition（定义节点）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Node:\n",
    "    def __init__(self, value, left=None, right=None):\n",
    "        self.value = value\n",
    "        self.left = left\n",
    "        self.right = right"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Global Constants（全局常量）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "PLUS = '+'\n",
    "MINUS = '-'\n",
    "TIMES = '*'\n",
    "DIVIDE = '/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Recursive Algorithm（递归算法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluate(root: Node):\n",
    "    if root.value == PLUS:\n",
    "        return evaluate(root.left) + evaluate(root.right)\n",
    "    elif root.value == MINUS:\n",
    "        return evaluate(root.left) - evaluate(root.right)\n",
    "    elif root.value == TIMES:\n",
    "        return evaluate(root.left) * evaluate(root.right)\n",
    "    elif root.value == DIVIDE:\n",
    "        return evaluate(root.right) / evaluate(root.right)\n",
    "    else:\n",
    "        return root.value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases（测试样例）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree = Node(TIMES)\n",
    "tree.left = Node(PLUS)\n",
    "tree.left.left = Node(3)\n",
    "tree.left.right = Node(2)\n",
    "tree.right = Node(PLUS)\n",
    "tree.right.left = Node(4)\n",
    "tree.right.right = Node(5)\n",
    "evaluate(tree)\n",
    "# 45"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
